package com.oc.mba.util;

import java.util.List;
import java.util.Map;

/**
 * @req ROADSHOW-SRS-company-001
 * @req ROADSHOW-SRS-company-002
 * @req ROADSHOW-SRS-securitiesDepartment-001
 */
public class PaginationSupport {
	public final static int		PAGESIZE	= Integer.valueOf(CommConsts.PAGESIZE);
	private int					pageSize	= PAGESIZE;
	private List				items;
	private int					totalCount;
	private int[]				indexes		= new int[] { 0 };
	private int					startIndex	= 0;
	private Map<String, Object>	resultMap;
	private String 				actionUrl;

	public PaginationSupport(List items, int totalCount) {
		initPageSize();
		setPageSize(PAGESIZE);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(0);
	}

	public PaginationSupport(List items, int totalCount, int startIndex) {
		initPageSize();
		setPageSize(PAGESIZE);
		initPageSize();
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
		initPageSize();
		setPageSize(pageSize);
		setTotalCount(totalCount);
		setItems(items);
		setStartIndex(startIndex);
	}

	public PaginationSupport(Map<String, Object> resultMap, int totalCount, int pageSize, int startIndex) {
		initPageSize();
		setPageSize(pageSize);
		setTotalCount(totalCount);
		setResultMap(resultMap);
		setStartIndex(startIndex);
	}

	private void initPageSize() {
		String pageSizeStr = null;
		// try{
		// PropertiesConfiguration config = new PropertiesConfiguration(getClass().getResource(Consts.APP_CONFIG_FILE));
		// pageSizeStr = config.getString("ec.pagesize");
		// }catch(Exception ex){
		// ex.printStackTrace();
		// }
		// if(pageSizeStr == null || pageSizeStr.equals("")){
		// pageSizeStr = "10";
		// }
		pageSizeStr = CommConsts.PAGESIZE;
		pageSize = Integer.parseInt(pageSizeStr);
	}

	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		if (totalCount > 0) {
			this.totalCount = totalCount;
			int count = totalCount / pageSize;
			if (totalCount % pageSize > 0)
				count++;
			indexes = new int[count];
			for (int i = 0; i < count; i++) {
				indexes[i] = pageSize * i;
			}
		}
		else {
			this.totalCount = 0;
		}
	}

	public int[] getIndexes() {
		return indexes;
	}

	public void setIndexes(int[] indexes) {
		this.indexes = indexes;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		if (totalCount <= 0)
			this.startIndex = 0;
		else if (startIndex >= totalCount)
			this.startIndex = indexes[indexes.length - 1];
		else if (startIndex < 0)
			this.startIndex = 0;
		else {
			this.startIndex = indexes[startIndex / pageSize];
		}
	}

	public int getNextIndex() {
		int nextIndex = getStartIndex() + pageSize;
		if (nextIndex >= totalCount)
			return getStartIndex();
		else
			return nextIndex;
	}

	public int getPreviousIndex() {
		int previousIndex = getStartIndex() - pageSize;
		if (previousIndex < 0)
			return 0;
		else
			return previousIndex;
	}

	/**
	 * @return
	 */
	public int getTotalPages() {
		int totalPages;
		if (getTotalCount() % getPageSize() > 0) {
			totalPages = getTotalCount() / getPageSize() + 1;
		}
		else {
			// totalPages = getTotalCount() / getPageSize();
			totalPages = getTotalCount() / getPageSize();
		}
		return totalPages;
	}

	/**
	 * @return
	 */
	public int getCurrentPage() {
		int currentPage = getStartIndex() / getPageSize() + 1;
		return currentPage;
	}

	/**
	 * @return
	 */
	public int getLastPage() {
		int lastPage = (getTotalPages() - 1) * getPageSize();
		return lastPage;
	}

	public Pagination getCurrentPagination() {

		Pagination pagination = new Pagination();
		pagination.setPageSize(this.pageSize);
		pagination.setPageNo(this.getCurrentPage());
		pagination.setTotalPage(this.getTotalPages());
		pagination.setPrevioustIndex(this.getPreviousIndex());
		pagination.setNextIndex(this.getNextIndex());
		pagination.setEndPageStartIndex(this.getLastPage());
		pagination.setTotalNum(this.totalCount);
		return pagination;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public String getActionUrl() {
		return actionUrl;
	}

	public void setActionUrl(String actionUrl) {
		this.actionUrl = actionUrl;
	}

}
